﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms.DataVisualization.Charting;

namespace YArchitech.Plumbing
{
	public class HYPumpGraphChart : Chart
	{
		public void Draw(List<stCurvePt> ptList, EPumpCurveType eType)
		{
			if (base.Series.Count < 1)
			{
				return;
			}
			this.ePumpType = eType;
			Series series = base.Series[0];
			series.Points.Clear();
			this.SetXYTitle("Q(m3/h)", this.GetYTitle(eType));
			ptList.Sort(new Comparison<stCurvePt>(HYPumpGraphChart.ComparePtByQ));
			foreach (stCurvePt stCurvePt in ptList)
			{
				series.Points.AddXY(stCurvePt.first, stCurvePt.second);
			}
		}

		public void Draw(HYPumpOperationCurveData pumpOpera, EPumpCurveType eType)
		{
			List<stCurvePt> list = new List<stCurvePt>();
			list.Clear();
			pumpOpera.GetSortedPtCurveArr(ref list);
			this.Draw(list, eType);
		}

		public void Draw(HYPumpData pumpData, EPumpCurveType eType)
		{
			if (pumpData.m_CurveQ2H.IsEmpty())
			{
				pumpData.AnalyseCurve(false);
			}
			switch (eType)
			{
			case EPumpCurveType.ePumpCurve_H:
				this.Draw(pumpData.m_CurveQ2H, eType);
				return;
			case EPumpCurveType.ePumpCurve_N:
				this.Draw(pumpData.m_CurveQ2N, eType);
				return;
			case EPumpCurveType.ePumpCurve_E:
				this.Draw(pumpData.m_CurveQ2Eff, eType);
				return;
			case EPumpCurveType.ePumpCurve_NPSH:
				this.Draw(pumpData.m_CurveQ2NPSH, eType);
				return;
			default:
				return;
			}
		}

		public void Draw(HYGroupPumpData groupData, EPumpCurveType eType)
		{
			if (groupData == null)
			{
				return;
			}
			if (groupData.m_GroupCurve_H == null || groupData.m_GroupCurve_H.m_arrGroupCurve == null || groupData.m_GroupCurve_H.m_arrGroupCurve.Count < 1)
			{
				groupData.AnalyseCurve(false);
				groupData.AnsysGroupCurve();
			}
			foreach (HYPumpOperationCurveData pumpOpera in groupData.GetGroupCurveAbout(eType).m_arrGroupCurve)
			{
				this.Draw(pumpOpera, eType);
			}
		}

		private static int ComparePtByQ(stCurvePt pt1, stCurvePt pt2)
		{
			if (pt1 == null || pt2 == null)
			{
				return 0;
			}
			if (pt1 == null)
			{
				return -1;
			}
			if (pt2 == null)
			{
				return 1;
			}
			if (pt1.first > pt2.first)
			{
				return 1;
			}
			if (pt1.first == pt2.first)
			{
				return 0;
			}
			return -1;
		}

		public void InitSeries()
		{
			foreach (Series series in base.Series)
			{
				series.BorderWidth = 2;
				series.IsVisibleInLegend = false;
				series.Color = Color.Sienna;
			}
		}

		public string GetYTitle(EPumpCurveType eType)
		{
			string result = "H(m)";
			switch (eType)
			{
			case EPumpCurveType.ePumpCurve_H:
				result = "H(m)";
				break;
			case EPumpCurveType.ePumpCurve_N:
				result = "N(kW)";
				break;
			case EPumpCurveType.ePumpCurve_E:
				result = "η(%)";
				break;
			case EPumpCurveType.ePumpCurve_NPSH:
				result = "NPSH(m)";
				break;
			}
			return result;
		}

		public void ShowXYTitle(bool bShow)
		{
			if (bShow)
			{
				this.SetXYTitle("Q(m3/h)", this.GetYTitle(this.ePumpType));
				return;
			}
			this.SetXYTitle("", "");
		}

		public void SetXYTitle(string szX, string szY)
		{
			foreach (ChartArea chartArea in base.ChartAreas)
			{
				chartArea.AxisX.Title = szX;
				chartArea.AxisY.Title = szY;
			}
		}

		public void SetMarker(bool bShow)
		{
			foreach (Series series in base.Series)
			{
				if (bShow)
				{
					series.MarkerStyle = MarkerStyle.Circle;
				}
				else
				{
					series.MarkerStyle = MarkerStyle.None;
				}
				series.IsValueShownAsLabel = bShow;
				if (bShow)
				{
					series.Label = "#VAL";
				}
				else
				{
					series.Label = null;
				}
			}
		}

		private EPumpCurveType ePumpType;
	}
}
